﻿using DigitalHealth.PACS.Common.Users;
using DigitalHealth.PACS.Cores.Middleware.Urls;
using DigitalHealth.PACS.UnifyPortalAggregateService.Dtos;
using DigitalHealth.PACS.UnifyPortalAggregateService.Models.AppointmentService;
using DigitalHealth.PACS.UnifyPortalAggregateService.Services.AppointmentService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net.Http;

namespace DigitalHealth.PACS.UnifyPortalAggregateService.Controllers
{
    /// <summary>
    /// 预约管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AppointmentController : ControllerBase
    {
        private readonly IClassOrderClient classOrderClient;
        private readonly IAppointClient appointClient;
        private readonly IDynamicMiddleUrl dynamicMiddleUrl;
        private readonly HttpClient httpClient;

        public AppointmentController(IClassOrderClient classOrderClient, IAppointClient appointClient, IDynamicMiddleUrl dynamicMiddleUrl, IHttpClientFactory httpClientFactory)
        {
            this.classOrderClient = classOrderClient;
            this.appointClient = appointClient;
            this.dynamicMiddleUrl = dynamicMiddleUrl;
            this.httpClient = httpClientFactory.CreateClient();
        }



        /// <summary>
        /// 根据排班医生查询可预约信息
        /// </summary>
        /// <param name="sysUser"></param>
        /// <param name="doctorId"></param>
        /// <returns></returns>
        [HttpGet("AppointmentInfo/ClassOrder/Doctor/{doctorId}")]
        public AppointmentInfoDto GetAppointmentInfos( string doctorId)
        {
            System.Console.WriteLine($"聚合服务--GetAppointmentInfos--{new Random().Next()}");
            AppointmentInfoDto result = new AppointmentInfoDto() { appointmentInfo=new List<AppointmentInfo>()};
            //1、查询医生用户

            //2、查询医生的排版信息
            var classOrders =classOrderClient.GetClassOrdersByDoctorId(doctorId);
            //3、查询可预约的排班信息
            foreach (var item in classOrders)
            {
                var appointmentInfos= appointClient.GetAppointmentsByClassOrder(item.Id.ToString());
                result.appointmentInfo.AddRange(appointmentInfos);
            }
            return result;
        }
    }
}
